using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PotonCapture
{
    public partial class SettingForm : Form
    {
        private MainForm _mainForm;

        public MainForm MainForm
        {
            get { return _mainForm; }
            set { _mainForm = value; }
        }
	

        public enum TargetRanges
        {
            FullScreen,
            Window,
            Client,
            EnumCount,
        }
        protected string[] TargetRangesName =
        {
            "Full Screen",
            "Window",
            "Client",
        };

		public enum ImageFormats
		{
			Bmp,
			Png,
			Jpg,
			EnumCount,
		}
		protected string[] ImageFormatsName =
        {
            "BMP",
            "PNG",
            "JPEG",
        };

		public enum CaptureKeys
		{
            None,
            PrintScreen,
            ScrollLock,
            Pause,
            MiddleButton,
            IMENonconvert,
            CapsLock,
            Insert,
            End,
            F1,
            F12,
//            CtrlShiftC,
            EnumCount,
		}
		protected string[] CaptureKeysName =
        {
            "",
            "Print Screen",
            "Scroll Lock",
            "Pause",
            "Middle Button",
            "IME Nonconvert",
            "Caps Lock",
            "Insert",
            "End",
            "F1",
            "F12",
//            "Ctrl+Shift+C",
        };


        public SettingForm()
        {
#if DEBUG
			System.Diagnostics.Debug.Assert(TargetRangesName.Length == (int)TargetRanges.EnumCount);
			System.Diagnostics.Debug.Assert(ImageFormatsName.Length == (int)ImageFormats.EnumCount);
			System.Diagnostics.Debug.Assert(CaptureKeysName.Length == (int)CaptureKeys.EnumCount);
#endif
			InitializeComponent();
        }

        private void SettingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && Visible)
            {
                if (MessageBox.Show(this, "Exit?", Application.ProductName, MessageBoxButtons.YesNo) != DialogResult.Yes)
                {
                    e.Cancel = true;
                }
            }
        }

        private void SettingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            _baseFolderPathCombo.Items.Add("");
            _baseFolderPathCombo.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            _baseFolderPathCombo.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
            _baseFolderPathCombo.Items.Add(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

            foreach (string name in TargetRangesName)
            {
                _targetRangeCombo.Items.Add(name);
            }
            _targetRangeCombo.SelectedIndex = 0;

			foreach (string name in ImageFormatsName)
            {
				_imageFormatCombo.Items.Add(name);
            }
			_imageFormatCombo.SelectedIndex = 0;

			foreach (string name in CaptureKeysName)
			{
				_captureKeyCombo.Items.Add(name);
			}
			_captureKeyCombo.SelectedIndex = 0;

		}

        private void SettingForm_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
            }
        }

        private void _okButton_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(_baseFolderPathCombo.Text))
            {
                MessageBox.Show(this, "Base folder path not found.", Application.ProductName, MessageBoxButtons.OK);
                return;
            }
            Properties.Settings.Default.BaseFolderPath = _baseFolderPathCombo.Text;
            Properties.Settings.Default.SaveFolderName = _saveFolderNameText.Text;
            Properties.Settings.Default.BaseFileName = _baseFileNameText.Text;
			Properties.Settings.Default.TargetRange = ((TargetRanges)_targetRangeCombo.SelectedIndex).ToString();
			Properties.Settings.Default.ImageFormat = ((ImageFormats)_imageFormatCombo.SelectedIndex).ToString();
            Properties.Settings.Default.CaptureKey = ((CaptureKeys)_captureKeyCombo.SelectedIndex).ToString();
            Properties.Settings.Default.ShowConfirm = _showConfirmCheck.Checked;
            Properties.Settings.Default.MemoryBeforeName = _memoryBeforeNameCheck.Checked;
            
            Properties.Settings.Default.Save();

            MainForm.UpdateSetting();

            Hide();
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            Hide();
        }

		private void SettingForm_Shown(object sender, EventArgs e)
		{
			System.Diagnostics.Debug.WriteLine("shown");

		}

		private void SettingForm_VisibleChanged(object sender, EventArgs e)
		{
			if (Visible)
			{
				System.Diagnostics.Debug.WriteLine("Visible");
				_baseFolderPathCombo.Text = Properties.Settings.Default.BaseFolderPath;
				_saveFolderNameText.Text = Properties.Settings.Default.SaveFolderName;
				_baseFileNameText.Text = Properties.Settings.Default.BaseFileName;

				string baseFolder = Properties.Settings.Default.BaseFolderPath;
				if (baseFolder.Length == 0)
				{
					baseFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
					_baseFolderPathCombo.Text = baseFolder;
				}

				string targetRange = Properties.Settings.Default.TargetRange;
				for (int index = 0; index < (int)TargetRanges.EnumCount; index++)
				{
					if (((TargetRanges)index).ToString() == targetRange)
					{
						_targetRangeCombo.SelectedIndex = index;
						break;
					}
				}
				string imageFormat = Properties.Settings.Default.ImageFormat;
				for (int index = 0; index < (int)ImageFormats.EnumCount; index++)
				{
					if (((ImageFormats)index).ToString() == imageFormat)
					{
						_imageFormatCombo.SelectedIndex = index;
						break;
					}
				}
				string captureKey = Properties.Settings.Default.CaptureKey;
				for (int index = 0; index < (int)CaptureKeys.EnumCount; index++)
				{
					if (((CaptureKeys)index).ToString() == captureKey)
					{
						_captureKeyCombo.SelectedIndex = index;
						break;
					}
				}
                _showConfirmCheck.Checked = Properties.Settings.Default.ShowConfirm;
                _memoryBeforeNameCheck.Checked = Properties.Settings.Default.MemoryBeforeName;
            }
		}
    }
}